package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiNiuYunUtil;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * 自定义job实现清理垃圾图片的功能
 */
public class ClearImgJob {
    /**
     * 清除redis中两个集合的差集，首先注入jedis操作集合
     */
    @Autowired
    private JedisPool jedisPool;
    public void clearImg(){
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        //set集合表示的是垃圾图片的集合
        if(set != null){
            for (String imgName : set) {
                //从七牛云存储和redis中删除垃圾图片
                QiNiuYunUtil.deleteFileFromQiniu(imgName);
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,imgName);
                //打印表示程序执行了
                System.out.println("执行自定义job"+imgName);
            }
        }
    }
}
